package com.swak.persistence.mybatis.filter;

import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Invocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Sql 的校验:
 * 
 * 1. update
 * 1.1 where条件为空
 * 1.2 没有使用主键或者唯一键
 * 
 * @author 618lf
 */
public class IllegalSQLCheckFilter implements Filter {

	Logger logger = LoggerFactory.getLogger(Filter.class);

	@Override
	public Object filter(Invocation invocation, FilterChain chain) throws Throwable {
		final Object[] queryArgs = invocation.getArgs();
		final MappedStatement mappedStatement = (MappedStatement) queryArgs[0];
		final Object parameterObject = queryArgs[1];
		BoundSql boundSql = mappedStatement.getBoundSql(parameterObject);
		String actualSql = boundSql.getSql();
		if (logger.isDebugEnabled()) {
			logger.debug("检查SQL是否合规，SQL:" + actualSql);
		}
		return chain.filter(invocation);
	}
}
